package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.PathVariable;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    Page<TrackListVo> selectUserTrackPage(Page<TrackListVo> trackListVoPage, TrackInfoQuery query);
    @Update("update track_info set order_num = order_num-1 , update_time = new() where album_id=#{albumId} and order_num>#{orderNum} and is_deleted=0")
    void updateOderNum(Long albumId, Integer orderNum);

    /**
     * 设计表：trackinfo * trackstat
     * 关系：一对多
     * 关联字段：track_id
     * inner join
     * album_id is_delete order_by track_id
     * 查询专辑声音分页列表
     * @param albumTrackListVoPage
     * @param albumId
     * @return
     */
    Page<AlbumTrackListVo> findAlbumTrackPage(Page<AlbumTrackListVo> albumTrackListVoPage,@Param("albumId") Long albumId);

    @Select("select album_id," +
            " max(if(stat_type='0401',stat_num,0)) playStatNum" +
            ",max(if(stat_type='0402',stat_num,0)) subscribeStatNum" +
            ",max(if(stat_type='0403',stat_num,0)) buyStatNum" +
            ",max(if(stat_type='0404',stat_num,0)) commentStatNum from album_stat where album_id=#{albumId} and is_deleted=0")
    AlbumStatVo getAlbumStatVo(@Param("albumId") Long albumId);

    @Select("select max(if(stat_type='0701',stat_num,0)) playStatNum" +
            ",max(if(stat_type='0702',stat_num,0)) collectStatNum" +
            ",max(if(stat_type='0703',stat_num,0)) praiseStatNum" +
            ",max(if(stat_type='0704',stat_num,0)) commentStatNum from track_stat where track_id=#{trackId} and is_deleted=0")
    TrackStatVo getTrackStatVo(@Param("trackId") Long trackId);
}
